package com.ysd.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ysd.entity.Category;
import com.ysd.service.CategoryService;
import com.ysd.util.Result;

@Controller
public class CategoryController {
	@Autowired
	CategoryService cService;
	@RequestMapping("getCategory")
	@ResponseBody
	public Object getCategory(Category category) {
		category.setPage((category.getPage()-1)*category.getLimit());
		List<Category> list=cService.findCategorys(category);
		int n=cService.getCount(category);
		if (list!=null) {
			return Result.toClient("0", "查询成功", n, list);
		} else {
			return Result.toClient("1", "查询失败", 0, null);
		}
	}
	@RequestMapping("delCategory")
	@ResponseBody
	public Object delCategory(int c_id) {
		int n=cService.deleteCategory(c_id);
		if (n>0) {
			return Result.toClient("0", "删除成功", 0, null);
		} else {
			return Result.toClient("1", "删除失败", 0, null);

		}
	}
	@RequestMapping("addCategory")
	@ResponseBody
	public Object addCategory(Category category) {
		Category c=cService.cChong(category);
		
		if (c!=null) {
			return Result.toClient("1", "当前分类名已存在", null);
		} else {
			int n=cService.insertCategory(category);
			if (n>0) {
				return Result.toClient("0", "添加成功", 0, null);
			} else {
				return Result.toClient("1", "添加失败", 0, null);
			}
		}
		
		
	}
	@RequestMapping("upCategory")
	@ResponseBody
	public Object upCategory(Category category) {
		
		Category c=cService.cChong(category);
		
		if (c!=null) {
			return Result.toClient("1", "当前分类名已存在", null);
		} else {
			int n=cService.updateCategory(category);
			if (n>0) {
				return Result.toClient("0", "修改成功", 0, null);
			} else {
				return Result.toClient("1", "修改失败", 0, null);
			}
		}
		
	}
	@RequestMapping("getType")
	@ResponseBody
	public Object getType() {
		List<Category> list=cService.findType();
		if (list!=null) {
			return Result.toClient("0", "查询成功", 0, list);
		} else {
			return Result.toClient("0", "查询失败", 0, null);
		}
	}
}
